var openId = wx.getStorageSync('openId');
var apiHost = wx.getStorageSync('apiHost');
var yunId = wx.getStorageSync('yunId');

var goodsType = "jd";
var goodsTypeAll = "all";
var orderType_z = "zonghe";         //综合排序
var orderType_t = "totalSalesDesc"; //销量排序
var orderType_c = "couponMoneyDesc";   //优惠券排序

var flagNum = 1;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag : true,
    jdGoodsList: [],       //京东商品集合
    JdListLength: '10',      //京东商品集合长度
    JdListStart: '0',      //京东商品集合开始
    categoryCode : '10000'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    var length = that.data.JdListLength;          //查询长度
    var Start = that.data.JdListStart;            //查询开始
    // var orderNum = that.data.orderTypeNum;        //点击
    // var orderType = that.getOrderTyep(orderNum);  //筛选条件
    // var categoryCode = that.data.categoryCode;    //筛选条件

    // that.cateGoryList();  //初始化分类
    that.advertList();    //初始化广告位
    that.specialTimeJd();                           
    // that.getJdGoodsList(Start, length, orderType, categoryCode); //京东优惠商品list

    //获取屏幕高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          jdGoodsHeght: res.windowHeight 
        });
      }
    });

  },

  // 点击分类“+” 显示隐藏
  showOrNo : function(){
    var that = this;
    var flag = that.data.flag
    flagNum = flagNum+1;

    if (flagNum%2==0){
      flag = false;
    }else{
      flag = true;
    }

    that.setData({
      flag: flag
    });
  },

  //获取商品分类
  cateGoryList : function(){

    var that = this;

    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/goods/categorys",
      data: {
        'type': '',
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg

        if (code == "10000") {
          var cateGoryList = res.data.data

          that.setData({
            cateGoryList: cateGoryList
          })
        }else{
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }

        wx.hideLoading();   //隐藏加载loading
      }
    })

  },

  //获取 广告位
  advertList: function () {

    var that = this;

    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/xcx/advert/list",
      data: {
        yunId: yunId,
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg

        if (code == "10000") {
          var advertList = res.data.data

          that.setData({
            advertList: advertList
          })
        }else{
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }

        wx.hideLoading();   //隐藏加载loading
      }
    })

  },

  /**
  * 获取京东商品集合
  */
  getJdGoodsList: function (start, length, orderTypea, categoryCode) {
    var that = this;

    //期间为了显示效果可以添加一个过度的弹出框提示“加载中” 
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "mobile/goods/list",
      data: {
        start: start,
        length: length,
        orderType: orderTypea,
        categoryCode: categoryCode
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg
  
        if (code == "10000") {
          var jdList = res.data.data.list;
          var jdGoodsListss = that.data.jdGoodsList;

          // 追加数据
          for (var i = 0; i < jdList.length; i++) {
            jdGoodsListss.push(jdList[i])
          }

          that.setData({
            jdGoodsList: jdGoodsListss
          })
        }else{
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  //获取排序方式
  getOrderTyep: function (orderNum) {
    switch (orderNum) {
      case '1':
        return orderType_z;
        break;
      case '2':
        return orderType_t;
        break;
      case '3':
        return orderType_c;
        break;
    }
  },

  //排序
  roderP: function (e) {
    var that = this;
    var orderT = e.target.dataset.toggle;
    var lengthflag = that.data.JdListLength;
    var startFlag = that.data.JdListStart;
    var categoryCode = that.data.categoryCode;

    if (startFlag == "0") {
      startFlag = '1';
    }

    var length = parseInt(lengthflag) * parseInt(startFlag);
    that.setData({
      orderTypeNum: orderT
    })

    var orderType = that.getOrderTyep(orderT);

    that.orderGoodsList('0', length, orderType, categoryCode); //京东优惠商品list

  },
  //排序京东商品列表
  orderGoodsList: function (start, length, orderType, categoryCode) {
    var that = this;

    //期间为了显示效果可以添加一个过度的弹出框提示“加载中” 
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "mobile/goods/list",
      data: {
        start: start,
        length: length,
        orderType: orderType,
        categoryCode: categoryCode
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
       
        var code = res.data.code;
        var msg = res.data.msg

        if ("10000" == code){
          var jdList = res.data.data.list;

          that.setData({
            jdGoodsList: jdList
          })
        }else{
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
        wx.hideLoading();   //隐藏加载loading

      }
    })
  },

  //分类点击事件
  cateGoryClick : function(e){
    var that = this;
    var cateGory = e.target.dataset.toggle;

    that.setData({
      categoryCode: cateGory
    });

    var length = that.data.JdListLength;          //查询长度
    var Start = that.data.JdListStart;            //查询开始
    var orderNum = that.data.orderTypeNum;        //点击
    var orderType = that.getOrderTyep(orderNum);  //筛选条件
    var categoryCode = that.data.categoryCode;

    that.orderGoodsList(Start, length, orderType, categoryCode); //京东优惠商品list

  },

  //分类点击事件
  cateGoryClick2: function (e) {
    var that = this;
    var cateGory = e.target.dataset.toggle;

    that.showOrNo();

    that.setData({
      categoryCode: cateGory
    });

    var length = that.data.JdListLength;          //查询长度
    var Start = that.data.JdListStart;            //查询开始
    var orderNum = that.data.orderTypeNum;        //点击
    var orderType = that.getOrderTyep(orderNum);  //筛选条件
    var categoryCode = that.data.categoryCode;

    that.orderGoodsList(Start, length, orderType, categoryCode); //京东优惠商品list

  },

  //跳转友惠特卖
  toYhspecial :function(){
    // 加载中
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.navigateTo({
      url: '../yhSpecial/yhSpecial?',
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },

  //跳转商品详情
  goJdDetail: function (e) {
    var that = this;
    var spuId = e.currentTarget.dataset.toggle;

    // 加载中
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.navigateTo({
      url: '../jdDetails/jdDetails?spuId=' + spuId,
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },


  //获取搜索框输入内容
  searchInput: function (e) {
    var that = this;
    that.setData({
      searchValue: e.detail.value
    })
  },

  //点击搜索按钮
  searchSubmit: function () {
    var that = this;
    var searVal = that.data.searchValue;    //获取搜索框内容

    // 加载中
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.navigateTo({
      url: '../yhSearchGoods/yhSearchGoods?searVal=' + searVal,
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },

  //获取京东特卖时间
  specialTimeJd : function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });
    wx.request({
      url: apiHost + "mobile/special/getspecialtime",
      data: {
        spuPlatform: goodsTypeAll
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {

        var code = res.data.code;
        var msg = res.data.msg;

        console.log(res.data.data)

        if (code == "10000") {
          var specialTimeList = res.data.data;

          

          var length = specialTimeList.length;
          var lengthFlag = length
          console.log(length)
          for (var i = 0; i < lengthFlag; i++) {

            var specialTimeOne = specialTimeList[i];

            var specialTime = specialTimeOne.specialTime;

            var specialTimeFlagList = specialTime.split(" ");
            var isSpecial = specialTimeOne.isSpecial;
            var isSpecialFlag = '';
            switch (isSpecial) {
              case 0:
                isSpecialFlag = "已开抢";
                break;
              case 1:
                isSpecialFlag = "特卖中";
                break;
              case 2:
                isSpecialFlag = "未开抢";
                break;
            }
            if (isSpecialFlag == "特卖中"){
              var Start = that.data.JdListStart;              //查询开始
              var length = that.data.JdListLength;            //查询长度
              var specialId = specialTimeOne.id;
              that.setData({
                specialId: specialId
              });

              that.specialGoodsList(Start, length, specialId)
             }
            specialTimeOne.isSpecial = isSpecialFlag
            specialTimeOne.specialTime = specialTimeFlagList[1].substr(0, 5)
            that.setData({ specialTimeList })
          }
          that.setData({
            specialTimeList: specialTimeList
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 点击特卖时间
   */
  onSpecialGoodsList: function (e){
    var that = this;

    that.setData({
      JdListStart: '0',
      JdListLength:'10'
    });

    var Start = that.data.JdListStart;              //查询开始
    var length = that.data.JdListLength;            //查询长度
    var specialId = e.currentTarget.dataset.toggle; //特卖id

    that.setData({
      specialId: specialId
    });

    that.specialGoodsList(Start,length,specialId)
  },

  /**
   * 获取京东特卖时间下的商品
   */
  specialGoodsList: function (start, length, specialId) {
    var that = this;

    //期间为了显示效果可以添加一个过度的弹出框提示“加载中” 
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "mobile/special/speciallist",
      data: {
        start: start,
        length: length,
        specialId: specialId,
        'type': goodsTypeAll
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {

        var code = res.data.code;
        var msg = res.data.msg

        if ("10000" == code) {
          var jdList = res.data.data.list;

          that.setData({
            jdGoodsList: jdList
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },
  /**
   * 特卖商品列表(下拉拼接)
   */
  specialGoodsListDown: function (start,length,specialId){
    var that = this;

    //期间为了显示效果可以添加一个过度的弹出框提示“加载中” 
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "mobile/special/speciallist",
      data: {
        start: start,
        length: length,
        specialId : specialId,
        'type' : goodsTypeAll
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg

        if (code == "10000") {
          var jdList = res.data.data.list;
          var jdGoodsListss = that.data.jdGoodsList;

          // 追加数据
          for (var i = 0; i < jdList.length; i++) {
            jdGoodsListss.push(jdList[i])
          }

          that.setData({
            jdGoodsList: jdGoodsListss
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },
  //滚动到底部触发事件  
  searchScrollLower: function () {
    var that = this;
    var length = that.data.JdListLength;
    var startFlag = that.data.JdListStart;
    var orderT = that.data.orderTypeNum;
    var nowStart = parseInt(startFlag) + 1

    // var categoryCode = that.data.categoryCode;
    var specialId = that.data.specialId;
    

    // var orderType = that.getOrderTyep(orderT);

    that.setData({
      JdListStart: nowStart
    });

    that.specialGoodsListDown(nowStart, length, specialId)

    // that.getJdGoodsList(nowStart, length, orderType, categoryCode); //京东优惠商品list

  },
})